﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;

namespace BackEnd
{
    public partial class RequestPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var language = Request.Form["language"];
            var query = Request.Form["query_string"];            
            var error = Request.Form["error"];
            var selectedLine = Request.Form["selectedLine"];
            var exceptionMessage = Request.Form["exceptionMessage"];
            var exceptionSource = Request.Form["exceptionSource"];

            List<KeyValuePair<double, Core.SearchResult>> res;

            Core.Analyzer analyzer = new Core.Analyzer();           

            string itemSerialized = "BEFORE_RUN";
            try
            {                
                if (string.IsNullOrEmpty(query)) res = analyzer.RunAnalysis(error, selectedLine, exceptionMessage, exceptionSource);
                else res = analyzer.RunAnalysis(query, "");

                var ser = new JavaScriptSerializer();
                itemSerialized = ser.Serialize(res);
            }
            catch (Exception exp)
            {
                Response.Write("Exception");
                Response.Write(exp.ToString());
                Response.Write("\n---------------------------------------------\n");
            }
            

            Response.Write(itemSerialized);
        }

        private void ThrowExceptionMethod() 
        {
            throw new Exception();
        }

    }
}